package net.ib.mtalk.ui.buddy.suggestion;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Session.AddBuddyReq;
import net.ib.mntalk.protocol.Session.AddContactsReq;
import net.ib.mntalk.protocol.Session.GetSuggestionsReq;
import net.ib.mntalk.protocol.Session.GetSuggestionsVersionReq;
import net.ib.mntalk.protocol.Session.SetSuggestionsReq;
import net.ib.mtalk.constant.MTalkConstant;
import net.ib.mtalk.network.task.GetSuggestionsTask;
import net.ib.mtalk.network.task.GetSuggestionsVersionTask;
import net.ib.mtalk.network.task.InsertBuddyTask;
import net.ib.mtalk.network.task.SetSuggestionTask;
import net.ib.mtalk.ui.UIController;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.MTalkLogWriter;
import android.content.Context;
import android.os.Handler;
import android.util.Log;

public class BuddySuggestionController extends UIController {
	
	private Context mContext;

	public BuddySuggestionController(Context context, Handler handler) {
		super(context, handler);
		this.mContext = context;
	}

	public void getSuggestions(GetSuggestionsReq getSuggestionsReq, long version) {
		try {
			MTalkLogWriter.d("BuddySuggestionController.getSuggestions()");
			GetSuggestionsTask task = new GetSuggestionsTask(mContext, mUiHandler, getSuggestionsReq, version);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "getSuggestions", e);
		}
	}

	public void getSuggestionVersion(GetSuggestionsVersionReq getSuggestionsVersionReq) {
		try {
			MTalkLogWriter.d("BuddySuggestionController.getSuggestionVersion()");
			GetSuggestionsVersionTask task = new GetSuggestionsVersionTask(mContext, mUiHandler, getSuggestionsVersionReq);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "getSuggestionVersion", e);
		}
	}
	
	public void addBuddy(AddBuddyReq addBuddyReq){
		try {
			MTalkLogWriter.d("BuddySuggestionController.addBuddy()");
			InsertBuddyTask task = new InsertBuddyTask(mContext, mUiHandler, addBuddyReq);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "addBuddy", e);
		}
	}

	public void deleteSuggestion(long msb, long lsb, String name) {
		try {
			MTalkLogWriter.d("BuddySuggestionController.deleteSuggestion()");
			SetSuggestionsReq setSuggestionsReq = new SetSuggestionsReq();
			setSuggestionsReq.addTarget(new Common.SuggestionTarget().setType(Common.USER_OBJ)
																	 .setObjectUuid(GpbSupport.makeUuidMsg(msb, lsb)));
			setSuggestionsReq.setStatusType(Common.SUGGESTION_STATUS_DELETE);
			SetSuggestionTask task = new SetSuggestionTask(mContext, mUiHandler, setSuggestionsReq, name);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "deleteSuggestion", e);
		}
	}

}
